package com.clouddot.baipai.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.http.entity.response.getCourseListResponse;

import java.util.ArrayList;

/**
 * Created by wxy on 2018/8/26.
 */

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.MyViewHolder> {
    private Context mContext;
    private ArrayList<getCourseListResponse> mDataList;

    public CourseAdapter(Context context, ArrayList<getCourseListResponse> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.h_adapter_coursename_item, parent, false));
        return holder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        getCourseListResponse bean = mDataList.get(position);
        if (null == bean) {
            return;
        }
        holder.name.setText(bean.CourseName);
        holder.name.setSelected(bean.flag);
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClicked(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDataList.size() > 0) {
            return mDataList.size();
        }
        return 0;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView name;


        public MyViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
        }
    }

    private AdapterItemClickListener listener;

    public void setOnClickListener(AdapterItemClickListener listener) {
        this.listener = listener;
    }

    public interface AdapterItemClickListener {
        void onClicked(int position);
    }


}
